import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class PingJiaShaiDanLogic extends GetxController {
  final state = PingJiaShaiDanState();

  @override
  void onInit() {
    super.onInit();
    state.orderInformationMap = Get.arguments;
    if (state.orderInformationMap['item_details'] != null) {
      state.orderGoodsList = state.orderInformationMap['item_details'];
    } else {
      state.orderGoodsList =
          state.orderInformationMap['distribution_order_item'];
      state.orderInformationMap['order_num_id'] =
          state.orderInformationMap['tml_num_id'];
    }
    print('state.orderGoodsList = ${(state.orderGoodsList ?? []).length}');

    (state.orderGoodsList ?? []).map((e) {
      Map<String, dynamic> map = {
        'controller': TextEditingController(text: ''),
        'goodsInt': 5,
        'logisticsServiceInt': 5,
        'serviceAttitudeInt': 5,
        'whetherAnonymous': true,
        // 'image': List<Asset>(),
        'image': List<AssetEntity>(),
        'imageUrl': '',
        'list_video': '',
        'series': e['series'],
      };
      state.commentsList.add(map);
    }).toList();

    print('state.commentsList = ${state.commentsList}');
    update();
  }

  void submitComments({
    BuildContext context,
  }) {
    List rateData = [];
    state.commentsList.map((item) {
      TextEditingController controller = item['controller'];
      Map<String, dynamic> map = {
        'tml_line': item['series'], //商品行号
        'content': controller.text, //评价内容
        'result': item['goodsInt'], //商品评价等级(1,2,3,4,5颗星)
        'logistics_type': item['logisticsServiceInt'], //物流评价等级(1,2,3,4,5颗星)
        'customer_service': item['serviceAttitudeInt'], //客服评价等级(1,2,3,4,5颗星)
        'anony': item['whetherAnonymous'], //是否匿名评价
        'rate_pic': item['imageUrl'], //图片
        'list_video': item['list_video'], //视频
      };
      rateData.add(map);
    }).toList();

    var params = {
      'tml_num_id': state.orderInformationMap['order_num_id'],
      'shop_id': state.orderInformationMap['ec_shop_num_id'],
      'tally_score': 5,
      'attitude_score': 5,
      'delivery_speed_score': 5,
      'logistics_service_score': 5,
      'access_token': SpUtil.getString('token'),
      'rate_data': rateData,
    };

    print(params);
    // print(state.orderInformationMap);
    // return;
    Api.submitComments(params).listen((event) {
      FToast fToast = FToast();
      fToast.init(context);
      if (event['code'] == 0) {
        fToast.showToast(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: setWidth(200),
                height: setWidth(200),
                decoration: BoxDecoration(
                  color: Color(0xff333333),
                  borderRadius: BorderRadius.circular(setWidth(20)),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.done_rounded,
                      color: Colors.white,
                      size: 40,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      '评论成功',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          gravity: ToastGravity.CENTER,
          toastDuration: Duration(seconds: 2),
        );
        Get.back(result: true);
        Get.toNamed(RouteConfig.viewComments,
            arguments: state.orderInformationMap);
      } else {
        fToast.showToast(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: setWidth(200),
                height: setWidth(200),
                decoration: BoxDecoration(
                  color: Color(0xff333333),
                  borderRadius: BorderRadius.circular(setWidth(20)),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.clear,
                      color: Colors.white,
                      size: 40,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      '评论失败',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          gravity: ToastGravity.CENTER,
          toastDuration: Duration(seconds: 2),
        );
      }
    });
  }
}
